package com.bw.module_store.view

import android.app.DatePickerDialog
import android.content.Intent
import android.graphics.Color
import android.view.View
import android.widget.DatePicker
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.module_store.R
import com.bw.module_store.adapter.AddPicAdapter
import com.bw.module_store.databinding.ActivityAddStoreBinding
import com.bw.module_store.entity.AddStoreParam
import com.bw.module_store.viewmodel.StoreViewModel
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import java.util.*
/**
 * 智慧园区-01-商家推荐-v1.1-吴熙-20250512
 */
@Route(path = RouterPath.PATH_ADD_STORE_ACTIVITY)
class AddStoreActivity : BaseMvvmActivity<ActivityAddStoreBinding>() {

    var selectDate:String=""
    lateinit var addPicAdapter: AddPicAdapter

    private val viewModel : StoreViewModel by viewModels()


    override fun initViews(){

        //添加图片
        addPicAdapter= AddPicAdapter()
        mDataBinding.rv.layoutManager=LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        mDataBinding.rv.adapter=addPicAdapter
        addPicAdapter.data.add("+")
        addPicAdapter.notifyDataSetChanged()

        //头布局
        mDataBinding.topbarAddStore.setBackgroundColor(Color.WHITE)
        mDataBinding.title.text="入驻申请"
        mDataBinding.title.setTextColor(Color.BLACK)
        mDataBinding.title2.visibility=View.VISIBLE
        mDataBinding.title2.text="申请记录"
        mDataBinding.title2.setTextColor(Color.parseColor("#5E9CE5"))
        mDataBinding.goAdd.visibility=View.GONE
        mDataBinding.goBack.visibility=View.GONE
        mDataBinding.goBackGray.visibility=View.VISIBLE
        mDataBinding.goBackGray.setOnClickListener { finish() }

        //选择入驻时间
        mDataBinding.ivSelectDate.setOnClickListener {
            val calendar: Calendar = Calendar.getInstance()
            val year: Int = calendar.get(Calendar.YEAR)
            val month: Int = calendar.get(Calendar.MONTH)
            val day: Int = calendar.get(Calendar.DAY_OF_MONTH)

            val datePickerDialog = DatePickerDialog(
                this,
                { view: DatePicker?, selectedYear: Int, selectedMonth: Int, selectedDay: Int ->
                    selectDate = selectedYear.toString() + "-" + (selectedMonth + 1) + "-" + selectedDay
                    mDataBinding.edStoreTime.setText(selectDate)
                },
                year, month, day
            )

            datePickerDialog.datePicker.minDate = System.currentTimeMillis() - 1000

            datePickerDialog.show()
        }

        //提交申请
        mDataBinding.btnCommit.setOnClickListener {
            viewModel.addStoreIn(
                AddStoreParam(
                applicationArea = mDataBinding.edStoreArea.text.toString().toInt(),
                applicationNumber = "RZ${System.currentTimeMillis()}",
                contactMethod = mDataBinding.edStoreTel.text.toString().toLong(),
                contactName = mDataBinding.edStorePersonName.text.toString(),
                documents = listOf("http://example.com/doc1.jpg", "http://example.com/doc2.jpg"),
                enterpriseName = mDataBinding.edStoreName.text.toString(),
                enterpriseProfileId = 1,
                entryPersonnel=mDataBinding.edStorePeopleCount.text.toString().toInt(),
                status = "新建",
                id=0,
                processingSteps= listOf(),
                entryTime=selectDate
            )
            )
        }

        //跳转到申请入驻列表
        mDataBinding.title2.setOnClickListener {
            ARouter.getInstance().build("/storeMerchant/storeMerchant").navigation()
        }

        //图片选择
        mDataBinding.layoutAddPic.setOnClickListener {
            PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE)
                .selectPicture()
        }

        //点击加号添加图片
        addPicAdapter.setOnItemClickListener { adapter, view, position ->
            if (addPicAdapter.data[position].equals("+")){
                PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE)
                    .selectPicture()
            }
        }

    }

    override fun observeDataChange() {
        viewModel.addStoreInSuccess.observe(this){
            ToastUtils.showLong("入驻成功！！")
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bundle = data!!.extras
            val bean = bundle!![PictureSelector.PICTURE_RESULT] as PictureBean?
            mDataBinding.layoutAddPic.visibility=View.GONE
            mDataBinding.rv.visibility=View.VISIBLE
            addPicAdapter.data.add(0,bean!!.path)
            addPicAdapter.notifyDataSetChanged()
        }
    }

    override fun getLayoutId(): Int =R.layout.activity_add_store

}